<template >
  <el-container class="home-container">
    <el-header>
      <div>
        <img
          src="../assets/booklogo.jpeg"
          alt=""
        />
        <span>无语图书店</span>
      </div>
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <el-container>
        <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse?'65px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          :default-active="$route.path"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse-transition="false"
          :collapse="isCollapse"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="icons[item.id]"></i>
              <!-- 文字 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <template>
                <i :class="icons[subItem.id]"></i>
                <span slot="title">{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menulist: [],
      icons:{
          125:'el-icon-hot-water',
          101:'el-icon-cold-drink',
          102:'el-icon-goblet-full',
          145:'el-icon-tableware',
          110:'el-icon-sugar',
          104:'el-icon-watermelon',
          108:'el-icon-cherry',
          121:'el-icon-ice-tea',
          107:'el-icon-ice-cream-round',
          146:'el-icon-lollipop'
      },
      isCollapse:false
    };
  },
  mounted() {
    let token = sessionStorage.getItem("token")
    let user = JSON.parse(token)
    if (user.username === 'admin') {
      this.$http.get("menus").then((rs) => {
        console.log(rs.data);
        this.menulist = rs.data;
      }).catch(err=>{
        this.$message.error(err)
      });
    }else{
      this.$http.get("menus").then(res=>{
        this.menulist = [res.data[2]]
      })
    }
  },
  methods: {
    //退出
    loginout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //侧边栏收缩
    toggleCollapse(){
        console.log(this.isCollapse);
        console.log(this.menulist);
        this.isCollapse = !this.isCollapse;
    }
  },
};
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
    background: #b3c0d1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 20px;
    color: #fff;
    > div {
      display: flex;
      align-items: center;
      img{
        width: 50px;
      }
      span {
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    background: #545c64;
    .el-menu{
        border: none;
    }
    .toggle-button{
        background: #545c64;
        text-align: center;
        line-height: 30px;
        color: #fff;
        font-size: 20px;
        font-weight: bold;
        // 设置字体间距
        letter-spacing: 0.3rem;
        cursor: pointer;
    }
  }
  .el-main {
    background: #e9eef3;
  }
}
</style>